"""
Case Type   : A兼容性-type功能增强
Case Name   : 使用抽象数据类型:创建抽象数据类型体和子类型体
Create At   : 2025/5/22
Owner       : wan005
Description :
    1.创建抽象类型
    2.创建抽象类型体
    3.创建抽象子类型
    4.创建抽象子类型体
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
History     :
"""
import os
import unittest
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Security(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'---{os.path.basename(__file__)} start---')
        self.userNode = Node(node='PrimaryDbAmode')
        self.sh_primy = CommonSH()
        self.common = Common()
        self.db_name = self.userNode.db_name
        self.type = 't1_035'
        self.sub_type = 't2_035'

    def test_security(self):
        text = '----step1:创建抽象类型;expect:成功----'
        self.logger.info(text)
        sql_cmd1 = f'create or replace type {self.type} as object(atri_pid ' \
                   f'number,atri_name varchar2(10),atri_sex varchar2(10),' \
                   f'static function func(num number) return number) not final;'
        res1 = self.sh_primy.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.logger.info(res1)
        self.assertEqual('CREATE TYPE', res1, '执行失败' + text)

        text = '----step2:创建抽象类型体;expect:成功----'
        self.logger.info(text)
        sql_cmd2 = f'''
        create or replace type body {self.type} as
        static function func(num number) return number as
        begin
        return num;
        end;
        end;
            '''
        res2 = self.sh_primy.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.logger.info(res2)
        self.assertEqual('CREATE TYPE', res2, '执行失败' + text)

        text = '----step3:创建抽象子类型;expect:成功----'
        self.logger.info(text)
        sql_cmd3 = f'create or replace type {self.sub_type} under {self.type}' \
                   f'(atri_job varchar2(10),atri_sal varchar2(10),static ' \
                   f'function func(num number) return number);'
        res3 = self.sh_primy.execut_db_sql(sql_cmd3, dbname=self.db_name)
        self.logger.info(res3)
        self.assertEqual('CREATE TYPE', res3, '执行失败' + text)

        text = '----step4:创建抽象子类型体;expect:成功----'
        self.logger.info(text)
        sql_cmd4 = f'''
        create or replace type body {self.sub_type} as
        static function func(num number) return number as
        begin
        return num;
        end;
        end;
        '''
        res4 = self.sh_primy.execut_db_sql(sql_cmd4, dbname=self.db_name)
        self.logger.info(res4)
        self.assertEqual('CREATE TYPE', res4, '执行失败' + text)

    def tearDown(self):
        text = '----step1:清理环境;expect:成功----'
        cle_cmd1 = f'drop type {self.sub_type};'
        cle_res1 = self.sh_primy.execut_db_sql(cle_cmd1, dbname=self.db_name)
        self.logger.info(cle_res1)
        self.assertEqual('DROP TYPE', cle_res1, text + '执行失败')

        cle_cmd2 = f'drop type {self.type};'
        cle_res2 = self.sh_primy.execut_db_sql(cle_cmd2, dbname=self.db_name)
        self.logger.info(cle_res2)
        self.assertEqual('DROP TYPE', cle_res2, text + '执行失败')
        self.logger.info(f'---{os.path.basename(__file__)} end---')
